package com.lms.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.lms.domain.NewProjects;
import com.lms.domain.Project;
import com.lms.domain.Role;
import com.lms.domain.User;
import com.lms.dto.ProjectDto;
import com.lms.dto.ProjectListDto;
import com.lms.dto.ProjectMapper;
import com.lms.dto.UserDto;
import com.lms.dto.UserListDto;
import com.lms.dto.UserMapper;
import com.lms.service.ProjectService;

@Controller
public class ProjectController extends MultiActionController {
	
	@Autowired
	private com.lms.service.UserService service;
	
	@Autowired
	private ProjectService projectService;
	
	@RequestMapping(value="/assignprojects",method = RequestMethod.GET)
	public String assignprojects(ModelMap model) {
 
		//model.addAttribute("message", "Spring 3 MVC Hello World");
		return "assignprojects";
 
	}

	@RequestMapping(value="/recordsP")
	public @ResponseBody UserListDto getUsersP() {

	UserListDto userListDto = new UserListDto();
	userListDto.setUsers(UserMapper.map(service.readAll()));
	return userListDto;
	}
	
	@RequestMapping(value="/updateP", method=RequestMethod.POST)
	public @ResponseBody UserDto updateP(
	@RequestParam String username,
	@RequestParam String firstName,
	@RequestParam String lastName,
	@RequestParam Integer role,
	@RequestParam Integer project) {

	Role existingRole = new Role();
	existingRole.setRole(role);

	Project existingProject = new Project();
	existingProject.setProject(project);

	User existingUser = new User();
	existingUser.setUsername(username);
	existingUser.setFirstName(firstName);
	existingUser.setLastName(lastName);
	existingUser.setRole(existingRole);
	existingUser.setProject(existingProject);

	return UserMapper.map(service.update(existingUser));
	}
	
	@RequestMapping(value = "/records1")
	public @ResponseBody
	ProjectListDto getProjects() {

		ProjectListDto projectListDto = new ProjectListDto();
		projectListDto.setProjects(ProjectMapper.map(projectService.readAll1()));
		return projectListDto;
	}
	
	@RequestMapping(value = "/create1", method = RequestMethod.POST)
	public @ResponseBody
	ProjectDto create1(
			@RequestParam String projectId,
			@RequestParam String projectName,
			@RequestParam String projectStartDate,
			@RequestParam String projectEndDate) {

		try{
			NewProjects newProject = new NewProjects();
			newProject.setProjectId(projectId);
			newProject.setProjectName(projectName);
			newProject.setProjectStartDate(projectStartDate);
			newProject.setProjectEndDate(projectEndDate);

			System.out.println("\n\n\n\n\nhhhhhhhhhhiiiiiiii\n\n\n\n");

			return ProjectMapper.mapProject(projectService.create1(newProject));
		}
		catch(Exception e) {
			return null;
		}
	}
	
	@RequestMapping(value = "/update1", method = RequestMethod.POST)
	public @ResponseBody ProjectDto update1(
			@RequestParam String projectId,
			@RequestParam String projectName,
			@RequestParam String projectStartDate,
			@RequestParam String projectEndDate) {
		System.out.println("\n\n\n\n\n\n\n\n\n\n\n");
		System.out.println(projectId);
		System.out.println("\n\n\n\n\n\n\n\n\n\n\n");
		NewProjects existingProject = new NewProjects();
		existingProject.setProjectId(projectId);
		existingProject.setProjectName(projectName);
		existingProject.setProjectStartDate(projectStartDate);
		existingProject.setProjectEndDate(projectEndDate);

		return ProjectMapper.mapProject(projectService.update1(existingProject));
	}
}
